package com.jnshu.task.app;

import com.jnshu.task.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MybatisTest {
    private SqlSession sqlSession;
    private UserMapper dao;
    private InputStream is;
    public SqlSessionFactory sqlSessionFactory;
    @BeforeAll
    public void init() throws IOException{
//1)读取配置文件
        is = Resources.getResourceAsStream("SqlMapConfig.xml");
//2)创建SqlSessionFactory
        SqlSessionFactoryBuilder sqlBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlFactory = sqlBuilder.build(is);
//3)使用工厂创建SqlSession对象 openSession(falshe) 为手动提交事务
        sqlSession = sqlFactory.openSession();
//4)使用SqlSession创建Dao接口的代理对象
        dao = sqlSession.getMapper(UserMapper.class);
    }

    //批量插入数据
    @Test
    public void TestBatchInsert() {
        List<User> users = new ArrayList<User>();
        int userCount = 10000;
        User user = new User();
        for (int i = 0; i != userCount; i++) {
            user.setUsername("聂小倩");
            user.setBirthday(new Date());
            user.setSex("女");
            user.setAddress("长沙");
            user.setCourse("UI");
            user.setEducation("本科");
            user.setPhone("1889220708");
            user.setSchool("深圳大学");
            user.setTerget("不工作就没有放吃");
            Date date = new Date();
            date.setTime(108L);
            user.setBirthday(date);
            users.add(user);
        }
            //按批次插入数据
        for (int j=0;j<2;j++) {
            dao.insertBatch(users);
        }
    }

    //根据名字查找表
    @Test
    public void TestFindUserByUsername() throws Exception{
        try{
            List<User> list = dao.findUserByUsername("邓科荣");
            for(int i=0;i<list.size();i++){
                System.out.println(list.get(i));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //根据学号来查用户信息
    @Test
    public void TestFindUserById() throws Exception{
        try {
            System.out.println(dao.findUserById(1));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //插入数据并返回主键id
    @Test
    public void TestInsertUser() throws Exception {
       try{

           User user = new User();
            user.setUsername("聂小倩");
            user.setBirthday(new Date());
            user.setSex("女");
            user.setAddress("长沙");
            user.setCourse("UI");
            user.setEducation("本科");
            user.setPhone("1889220708");
            user.setSchool("深圳大学");
            user.setTerget("不工作就没有放吃");
            Date date = new Date();
            date.setTime(108L);
            user.setBirthday(date);
            dao.insertUser(user);
            System.out.println("index:"+user.getId());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //修改用户信息
@Test
public void TestUpdateUser() throws Exception {

    try {
        //修改id为3的宣言
        User user = dao.findUserById(3);
        System.out.println(user.getTerget());
        user.setTerget("老大最帅");
        dao.updateUser(user);
    }catch (Exception e){
        e.printStackTrace();
    }
}
    @Test
    public void TestDeleteById() throws Exception{
     try {   //删除id为3的用户
         dao.deleteUserById(69453043);
     }catch (Exception e){
         e.printStackTrace();
     }
    }

    @AfterAll
    public void close() throws IOException{
        //清理缓存
        sqlSession.clearCache();
//这里需要提交事务
        sqlSession.commit();
//6)释放资源
        sqlSession.close();
        is.close();

    }
}